<?php
/**
 * AttendanceController.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-04-08 17:31:31
 * @modified    2023-04-08 17:31:31
 *
 */

namespace App\Http\Controllers;

use App\Services\AttendanceService;
use Illuminate\Http\Request;

class AttendanceController extends BaseModuleController
{
    /**
     * @OA\Get(
     *     path="/attendance/{attendanceId}",
     *     tags={"Attendance"},
     *     summary="根据打卡ID获取打卡详情",
     *     description="根据打卡ID获取打卡详情",
     *     operationId="attendance_show",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="attendanceId", in="path", description="签到ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function show(Request $request, $attendanceId)
    {
        try {
            $attendanceId = intval($attendanceId);
            $attendance = AttendanceService::fetch($attendanceId);
            return message('获取成功', true, $attendance);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/attendance/work_order/{workorder_id}",
     *     tags={"Attendance"},
     *     summary="根据工单ID获取签到列表",
     *     description="根据工单ID获取签到列表",
     *     operationId="attendance_work_order",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="workorder_id", in="path", description="工单ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function byWorkOrderId(Request $request, $workOrderId)
    {
        try {
            $list = AttendanceService::getListByWorkOrderId($workOrderId);
            return message('获取成功', true, $list);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/attendance/page",
     *     tags={"Attendance"},
     *     summary="分页获取打卡列表",
     *     description="分页获取打卡列表",
     *     operationId="attendance_page",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="name", in="query", description="签到人姓名", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="sign_in_date", in="query", description="根据签到日期查询  yyyy-MM-dd", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="page", in="query", description="当前页数", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="limit", in="query", description="每页数量(默认10)", required=false, @OA\Schema(default=10, type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function page(Request $request)
    {
        try {
            $list = AttendanceService::getList();
            return list_message('获取成功', true, $list);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/attendance",
     *     tags={"Attendance"},
     *     summary="签到或签退数据提交",
     *     description="签到或签退数据提交",
     *     operationId="attendance_store",
     *     security={{"bearerAuth":{}}},
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"id", "checkin_address", "checkout_address", "checkin_img", "checkout_img", "check_status", "checkin_time", "checkout_time", "rel_checkout_time", "checkout_delay_desc", "work_order_ids"},
     *              @OA\Property(property="id", type="integer", example="打卡ID，如果是签退，该ID必填", description=""),
     *              @OA\Property(property="checkin_address", type="string", example="签到地址", description=""),
     *              @OA\Property(property="checkout_address", type="string", example="签退地址", description=""),
     *              @OA\Property(property="checkin_img", type="string", example="签到图片", description=""),
     *              @OA\Property(property="checkout_img", type="string", example="签退图片", description=""),
     *              @OA\Property(property="check_status", type="string", example="签到状态，IN=签到，OUT=签退", description=""),
     *              @OA\Property(property="checkin_time", type="string", example="签到时间", description=""),
     *              @OA\Property(property="checkout_time", type="string", example="签退时间", description=""),
     *              @OA\Property(property="rel_checkout_time", type="string", example="真实签退时间", description=""),
     *              @OA\Property(property="checkout_delay_desc", type="string", example="签退延迟原因", description=""),
     *              @OA\Property(property="work_order_ids", type="array", @OA\Items())
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function store()
    {
        try {
            $attendance = AttendanceService::store();
            return message('操作成功', true, $attendance);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }

    }
}